import 'dart:async';

import 'package:connectivity_plus/connectivity_plus.dart';

/// 订阅者回调网络状态
typedef ConnectivityResultCallBack = void Function(ConnectivityResult result);

class CXConnectivityUtils {
  /// 保存单例，被标记为 late 的变量 _instance 的初始化操作将会延迟到字段首次被访问时执行，而不是在类加载时就初始化
  static final CXConnectivityUtils _instance = CXConnectivityUtils._internal();

  /// 网络监听相关类
  final Connectivity _connectivity = Connectivity();

  /// 当前网络状态，默认为无网络
  ConnectivityResult connectionStatus = ConnectivityResult.none;

  /// 网络监听订阅
  StreamSubscription<ConnectivityResult>? _connectivitySubscription;

  /// 监听回调集合
  final Map<String, ConnectivityResultCallBack> _callbackTable = {};

  /// 私有构造函数
  CXConnectivityUtils._internal() {
    _initConnectivity();
  }

  /// 获取单例方法
  static getInstance() => _instance;

  /// 初始化网络监听
  void _initConnectivity() {
    _connectivitySubscription =
        _connectivity.onConnectivityChanged.listen((result) {
      if (connectionStatus != result) {
        connectionStatus = result;
        emit();
      }
    });
  }

  /// 获取当前网络状态
  Future<ConnectivityResult> checkConnectivity() async {
    ConnectivityResult result = await _connectivity.checkConnectivity();
    connectionStatus = result;
    return result;
  }

  /// 增加监听者
  void addListen(String name, ConnectivityResultCallBack callBack) {
    // 如果网络监听订阅为空，则初始化网络监听
    _callbackTable[name] = callBack;
    if (_connectivitySubscription == null) {
      _initConnectivity();
    }
  }

  // 移除监听者
  void removeListen(String name) {
    if (_callbackTable[name] != null) {
      _callbackTable.removeWhere((key, value) => key == name);
    }
  }

  /// 取消网络监听
  void dispose() {
    _connectivitySubscription?.cancel();
    _connectivitySubscription = null;
    _callbackTable.clear();
  }

  /// 发送通知
  void emit() {
    // 反向遍历，防止订阅者在回调中移除自身带来的下标错位
    _callbackTable.forEach((key, value) {
      value(connectionStatus);
    });
  }
}
